keystone-pkg:
  pkg.installed:
    - names:
      - openstack-keystone
      - httpd
      - mod_wsgi
      - python-memcached
      - python2-openstackclient

keystone-conf:
  file.managed:
    - name: /etc/keystone/keystone.conf
    - source: salt://openstack/queen/keystone/files/keystone.conf
    - user: root
    - group: keystone
    - mode: 640
    - template: jinja
    - defaults:
      MYSQL_IP: {{ pillar['mysql']['MYSQL_IP'] }}
      KEYSTONE_DB_PASS: {{ pillar['keystone']['KEYSTONE_DB_PASS'] }}
      KEYSTONE_DB_USER: {{ pillar['keystone']['KEYSTONE_DB_USER'] }}
      KEYSTONE_DB_NAME: {{ pillar['keystone']['KEYSTONE_DB_NAME'] }}
    - require:
      - pkg: keystone-pkg


keystone-fernet-key:
  file.managed:
    - name: /etc/script/keystone-fernet-key.sh
    - source: salt://openstack/queen/keystone/files/keystone-fernet-key.sh
    - user: root
    - group: root
    - mode: 700
    - require:
      - pkg: keystone-pkg
  cmd.run:
    - name: bash /etc/script/keystone-fernet-key.sh && touch /etc/lock/keystone-fernet-key.lock 
    - require:
      - file: keystone-fernet-key
    - unless : test -f /etc/lock/keystone-fernet-key.lock

keystone-db-sync:
  cmd.run:
    - name: su -s /bin/sh -c "keystone-manage db_sync" keystone && touch /etc/lock/keystone-db-sync.lock
    - require:
      - file: keystone-conf
    - unless : test -f /etc/lock/keystone-db-sync.lock

keyston-bootstrap:
  file.managed:
    - name: /etc/script/keystone-bootstrap.sh
    - source: salt://openstack/queen/keystone/files/keystone-bootstrap.sh
    - user: root
    - group: root
    - mode: 700
    - template: jinja
    - defaults:
      KEYSTONE_ADMIN_PASS: {{ pillar['keystone']['KEYSTONE_ADMIN_PASS'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}
  cmd.run:
    - name: bash /etc/script/keystone-bootstrap.sh && touch /etc/lock/keystone-bootstrap.lock 
    - require:
      - file: keystone-conf
      - cmd: keystone-db-sync
    - unless : test -f /etc/lock/keystone-bootstrap.lock


keystone-httpd-conf:
  file.managed:
    - name: /etc/httpd/conf/httpd.conf
    - source: salt://openstack/queen/keystone/files/httpd.conf
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      IP_ADDR: {{ pillar['keystone']['KEYSTONE_IP'] }}
    - require:
      - pkg: keystone-pkg

keystone-wsgi-conf:
  file.symlink:
    - name: /etc/httpd/conf.d/wsgi-keystone.conf
    - target: /usr/share/keystone/wsgi-keystone.conf
    - require:
      - pkg: httpd

keystone-service:
  service.running:
    - name: httpd
    - enable: True
    - reload: True
    - require:
      - file: keystone-httpd-conf
      - file: keystone-wsgi-conf
      - file: keystone-conf
    - watch:
      - file: keystone-httpd-conf

keystone-profile-admin:
  file.managed:
    - name: /root/admin-openstack.sh
    - source: salt://openstack/queen/keystone/files/admin-openstack.sh
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      KEYSTONE_ADMIN_PASS: {{ pillar['keystone']['KEYSTONE_ADMIN_PASS'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}

keystone-profile-tenant:
  file.managed:
    - name: /root/demo-openstack.sh
    - source: salt://openstack/queen/keystone/files/demo-openstack.sh
    - user: root
    - group: root
    - mode: 644
    - template: jinja
    - defaults:
      KEYSTONE_TENANT_PROJECT: {{ pillar['keystone']['KEYSTONE_TENANT_PROJECT'] }}
      KEYSTONE_TENANT_USER: {{ pillar['keystone']['KEYSTONE_TENANT_USER'] }}
      KEYSTONE_TENANT_PASS: {{ pillar['keystone']['KEYSTONE_TENANT_PASS'] }}
      KEYSTONE_IP: {{ pillar['keystone']['KEYSTONE_IP'] }}


keystone-role-user:
  file.managed:
    - name: /etc/script/keystone-role-user.sh
    - source: salt://openstack/queen/keystone/files/keystone-role-user.sh
    - user: root
    - group: root
    - mode: 700
    - template: jinja
    - defaults:
      KEYSTONE_TENANT_PROJECT: {{ pillar['keystone']['KEYSTONE_TENANT_PROJECT'] }}
      KEYSTONE_TENANT_USER: {{ pillar['keystone']['KEYSTONE_TENANT_USER'] }}
      KEYSTONE_TENANT_PASS: {{ pillar['keystone']['KEYSTONE_TENANT_PASS'] }}
      GLANCE_KEYSTONE_USER : {{ pillar['glance']['GLANCE_KEYSTONE_USER'] }}
      GLANCE_KEYSTONE_PASS: {{ pillar['glance']['GLANCE_KEYSTONE_PASS'] }}
      NOVA_KEYSTONE_USER: {{ pillar['nova']['NOVA_KEYSTONE_USER'] }}
      NOVA_KEYSTONE_PASS: {{ pillar['nova']['NOVA_KEYSTONE_PASS'] }}
      PLACEMENT_KEYSTONE_USER: {{ pillar['placement']['PLACEMENT_KEYSTONE_USER'] }}
      PLACEMENT_KEYSTONE_PASS: {{ pillar['placement']['PLACEMENT_KEYSTONE_PASS'] }}
      NEUTRON_KEYSTONE_USER: {{ pillar['neutron']['NEUTRON_KEYSTONE_USER'] }}
      NEUTRON_KEYSTONE_PASS: {{ pillar['neutron']['NEUTRON_KEYSTONE_PASS'] }}
      CINDER_KEYSTONE_USER: {{ pillar['cinder']['CINDER_KEYSTONE_USER'] }}
      CINDER_KEYSTONE_PASS: {{ pillar['cinder']['CINDER_KEYSTONE_PASS'] }}
  cmd.run:
    - name: bash /etc/script/keystone-role-user.sh && touch /etc/lock/keystone-role-user.lock
    - require:
      - file: keystone-role-user
      - file: keystone-profile-admin
      - service: keystone-service
    - unless : test -f /etc/lock/keystone-role-user.lock 
